import json
import random
import requests

from lxml import etree

from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from django_redis import get_redis_connection

import redis
from utils.cookies_pool.cookie_pool import Pools


from .login_tianyancha import TianYanCha
from utils.json_func import to_json_data
# Create your views here.

class LoginTianYan(View):
    def get(self, request):
        # 获取用户数据
        mobile = request.GET.get('username')
        if not mobile:
            return to_json_data(errno=1, errmsg='传入手机号')
        password = request.GET.get('password')
        if not password:
            return to_json_data(errno=1, errmsg='传入密码')

        # 开始登录
        tianyan = TianYanCha(username=mobile, password=password)
        data = tianyan()
        info = json.loads(data)
        if info.get('errno') == 0:
            try:
                redis_key = f'mobile_{mobile}'
                connect = get_redis_connection('cookie')
                mapping = {
                    'mobile': mobile,
                    'password': password,
                    'cookie': info.get('data'),
                    'request_num': 0
                }
                connect.hmset(redis_key, mapping=mapping)
                return to_json_data(errmsg='登录成功', data=info.get('data'))
            except:
                print('存储失败')
        else:
            requests.get(f'http://127.0.0.1:9057/spider/login/?username={mobile}&password={password}')
            return to_json_data(errmsg='登录失败')


class Test(View):
    def get(self, request):
        # connect = get_redis_connection('cookie')
        # data = connect.get('mobile_13853275090')
        # data = connect.keys()
        # pools = Pools()
        # data = pools.get_all_keys()
        #
        # print(data)
        # print(random.choice(data))
        # print(type(data))
        # connect.setex('mobile_13853275090', 60, 'bb')
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
            'Cookie': 'ssuid=5385309444; TYCID=646c8f80a2b411e9b364ff90ae1b0927; undefined=646c8f80a2b411e9b364ff90ae1b0927; _ga=GA1.2.474310100.1562723129; aliyungf_tc=AQAAAKe4CEEcRAkA2mGDfBfSXQjCPFWa; csrfToken=23AViI-vOfL0VYeJr8yEHuSQ; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1563441215,1563502650,1563507065,1563860022; _gid=GA1.2.2001517937.1563860022; bannerFlag=true; token=70b5fe59b7fb4fe8a42185c763e15ddb; _utm=8eaf7e55a26b405c97a04479c52b4dcf; RTYCID=83f662c7ecca49f99c1fbd8400e60490; CT_TYCID=cc3a925f0d52401f9245abda94566a36; _gat_gtag_UA_123487620_1=1; tyc-user-info=%257B%2522claimEditPoint%2522%253A%25220%2522%252C%2522myAnswerCount%2522%253A%25220%2522%252C%2522myQuestionCount%2522%253A%25220%2522%252C%2522signUp%2522%253A%25220%2522%252C%2522explainPoint%2522%253A%25220%2522%252C%2522privateMessagePointWeb%2522%253A%25220%2522%252C%2522nickname%2522%253A%2522%25E5%2585%258B%25E9%2587%258C%25E6%2596%25AF%25E8%2592%2582%25E5%25A8%259C%25C2%25B7%25E8%2589%25BE%25E4%25BC%25AF%25E7%259B%2596%25E7%2589%25B9%2522%252C%2522integrity%2522%253A%25220%2525%2522%252C%2522privateMessagePoint%2522%253A%25220%2522%252C%2522state%2522%253A%25220%2522%252C%2522announcementPoint%2522%253A%25220%2522%252C%2522isClaim%2522%253A%25220%2522%252C%2522vipManager%2522%253A%25220%2522%252C%2522discussCommendCount%2522%253A%25221%2522%252C%2522monitorUnreadCount%2522%253A%25228%2522%252C%2522onum%2522%253A%25220%2522%252C%2522claimPoint%2522%253A%25220%2522%252C%2522token%2522%253A%2522eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzg1MzI3NTA5MCIsImlhdCI6MTU2Mzg3MzE0NSwiZXhwIjoxNTk1NDA5MTQ1fQ.zz-bme4IQ3Pve1PiIqh8M1c2VzRsq9KvFWeIe_HZOuhou7DSdpruq834iLQbsvHX5QJCuU5SWP1OQ1uFJlgIMQ%2522%252C%2522pleaseAnswerCount%2522%253A%25220%2522%252C%2522redPoint%2522%253A%25220%2522%252C%2522bizCardUnread%2522%253A%25220%2522%252C%2522vnum%2522%253A%25220%2522%252C%2522mobile%2522%253A%252213853275090%2522%257D; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzg1MzI3NTA5MCIsImlhdCI6MTU2Mzg3MzE0NSwiZXhwIjoxNTk1NDA5MTQ1fQ.zz-bme4IQ3Pve1PiIqh8M1c2VzRsq9KvFWeIe_HZOuhou7DSdpruq834iLQbsvHX5QJCuU5SWP1OQ1uFJlgIMQ; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1563873174; cloud_token=5a6de244f4a14187830152869cd644d3; cloud_utm=bdec375ebec342fcb95b1c8034142e4c'
        }
        resp = requests.get('https://www.tianyancha.com/company/2349015448', headers=headers)
        print(resp.text)
        html = etree.HTML(resp.text)
        name = html.xpath('//h1/text()')
        dic = {
            'name': name
        }
        return HttpResponse(json.dumps(dic))


